package com.ruoyi.consumption.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.consumption.dto.ConsumptionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.consumption.mapper.ConsumptionMapper;
import com.ruoyi.consumption.domain.Consumption;
import com.ruoyi.consumption.service.IConsumptionService;

/**
 * 能耗Service业务层处理
 *
 * @author hcc
 * @date 2024-11-25
 */
@Service
public class ConsumptionServiceImpl extends ServiceImpl<ConsumptionMapper, Consumption> implements IConsumptionService //修改
{
    @Autowired
    private ConsumptionMapper consumptionMapper;

    /**
     * 查询能耗
     *
     * @param consumptionId 能耗主键
     * @return 能耗
     */
    @Override
    public Consumption selectConsumptionByConsumptionId(String consumptionId) {
        return consumptionMapper.selectConsumptionByConsumptionId(consumptionId);
    }

    /**
     * 查询能耗列表
     *
     * @param consumption 能耗
     * @return 能耗
     */
    @Override
    public List<Consumption> selectConsumptionList(Consumption consumption) {
        return consumptionMapper.selectConsumptionList(consumption);
    }

    /**
     * 新增能耗
     *
     * @param consumption 能耗
     * @return 结果
     */
    @Override
    public int insertConsumption(Consumption consumption) {
        return consumptionMapper.insertConsumption(consumption);
    }

    /**
     * 修改能耗
     *
     * @param consumption 能耗
     * @return 结果
     */
    @Override
    public int updateConsumption(Consumption consumption) {
        return consumptionMapper.updateConsumption(consumption);
    }

    /**
     * 批量删除能耗
     *
     * @param consumptionIds 需要删除的能耗主键
     * @return 结果
     */
    @Override
    public int deleteConsumptionByConsumptionIds(String[] consumptionIds) {
        return consumptionMapper.deleteConsumptionByConsumptionIds(consumptionIds);
    }

    /**
     * 删除能耗信息
     *
     * @param consumptionId 能耗主键
     * @return 结果
     */
    @Override
    public int deleteConsumptionByConsumptionId(String consumptionId) {
        return consumptionMapper.deleteConsumptionByConsumptionId(consumptionId);
    }

    @Override
    public ConsumptionDto getData() {
        ConsumptionDto consumptionDto = new ConsumptionDto();
        List<Date> dataList = consumptionMapper.getTime();
        List<String> collect = dataList.stream().map(date -> {
            String dateFormat = "yyyy-MM-dd HH:mm:ss";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
            LocalDateTime dateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            String formattedDateTime = dateTime.format(formatter);
            return formattedDateTime;
        }).collect(Collectors.toList());
        List<Double> results = new ArrayList<>();
        Date conDate = null;
        for (Date date : dataList) {
            long between = 0l;
            if (conDate != null) {
                between = TimeUnit.MILLISECONDS.toSeconds(date.getTime() - conDate.getTime());
            } else {
                between = 300l;
            }
            conDate = date;
            Consumption consumption = consumptionMapper.getDataList(date);
            if (consumption != null){
                Float analog1Value = Float.valueOf((consumption.getAh1Analog1()));
                Float analog4Value = Float.valueOf((consumption.getAh1Analog4()));
                Float analog1Ah32 = Float.valueOf((consumption.getAh32Analog1()));
                Float analog4Ah32 = Float.valueOf((consumption.getAh32Analog4()));
                double result = (analog1Value * analog4Value + analog1Ah32 * analog4Ah32) * between;
                String format = String.format("%.2f", result);
                results.add(Double.valueOf(format));
            }
        }
        consumptionDto.setConsumptionList(results);
        consumptionDto.setDataList(collect);
        return consumptionDto;
    }
}
